package cn.yuhe.system.controller;

import cn.yuhe.basic.annotation.PreAuthorize;
import cn.yuhe.basic.utils.AjaxResult;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.system.domain.DictionaryDetail;
import cn.yuhe.system.query.DictionaryDetailQuery;
import cn.yuhe.system.query.DictionaryTypeQuery;
import cn.yuhe.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 控制层
* @Author: Mr.Qiu
* @Version: 1.0
* @Date:
*/
@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /** 
    * @Title: 根据id查询
    * @Description:
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:13  
    * @Parameters: [id]
    * @Return cn.yuhe.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "根据id查询数据字典",sn = "dictionaryDetail:loadById")
    public DictionaryDetail loadById(@ApiParam(value="部门表属性",required = true) @PathVariable("id") Long id){

        return dictionaryDetailService.loadById(id);
    }

    /**
    * @Description: 查询所有
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:
    */
    @GetMapping
    @PreAuthorize(name = "查询所有数据字典",sn = "dictionaryDetail:loadAll")
    public List<DictionaryDetail> loadAll(){

        return dictionaryDetailService.loadAll();
    }

    /** 
    * @Title: 根据id删除
    * @Description: 根据id删除
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:53  
    * @Parameters: []
    * @Return java.util.List<cn.yuhe.org.domain.Department>
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除数据字典",sn = "dictionaryDetail:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
           dictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: 新增修改
    * @Description: 新增修改
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 12:56
    * @Parameters: [department]
    * @Return cn.yuhe.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "新增修改数据字典",sn = "dictionaryDetail:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null) {
                 dictionaryDetailService.save(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    @PostMapping
    @PreAuthorize(name = "分页高级查询数据字典",sn = "dictionaryDetail:querData")
    public PageList<DictionaryDetail> querData(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
            PageList<DictionaryDetail> data = dictionaryDetailService.queryData(dictionaryDetailQuery);
                return data;
    }




    @PatchMapping
    @PreAuthorize(name = "数据字典批量删除",sn = "dictionaryDetail:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dictionaryDetailService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
